<template>
  <div class="login-container">
    <div class="login-container-content">
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        auto-complete="on"
        label-position="left"
      >
        <div class="login-container-content-logo">
          <img src="@/assets/login/pr_logo.png" alt="" />
        </div>
        <div class="title-container">
          <h2 class="title">数据标识和保护系统</h2>
        </div>
        <div class="login-container-content-form">
          <el-form-item prop="username">
            <span class="svg-container">
              <svg-icon icon-class="user" />
            </span>
            <el-input
              class="input-btn"
              ref="username"
              v-model="loginForm.username"
              placeholder="请输入用户名"
              name="username"
              tabindex="1"
              type="text"
              auto-complete="on"
            />
          </el-form-item>
          <el-form-item prop="password">
            <span class="svg-container">
              <svg-icon icon-class="password" />
            </span>
            <el-input
              class="input-btn"
              :key="passwordType"
              ref="password"
              v-model="loginForm.password"
              :type="passwordType"
              placeholder="请输入密码"
              name="password"
              tabindex="2"
              auto-complete="on"
              @keyup.enter.native="handleLogin"
            />
            <span class="show-pwd" @click="showPwd">
              <svg-icon
                :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
              />
            </span>
          </el-form-item>
          <el-button
            :loading="loading"
            type="primary"
            class="login-btn"
            @click.native.prevent="handleLogin"
            >登录</el-button
          >
        </div>
      </el-form>
    </div>
    <div class="foot">
      <img src="@/assets/login/cetc.png" alt="" />
      中国电子科技网络信息安全有限公司
    </div>
  </div>
</template>

<script>
import router, { createRouter } from "@/router";
import store from "@/store";
import { mapRouter } from "@/router/mapRouter";
import { constantRoutes } from "@/router/routerList";
export default {
  name: "Login",
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!value) {
        callback(new Error("请输入正确的用户名"));
      } else {
        callback();
      }
    };
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error("密码不能少于6位"));
      } else {
        callback();
      }
    };
    return {
      loginForm: {
        // 用户名与密码
        username: "",
        password: "",
      },
      loginRules: {
        // 表单验证规则
        username: [
          { required: true, trigger: "blur", validator: validateUsername },
        ],
        password: [
          { required: true, trigger: "blur", validator: validatePassword },
        ],
      },
      loading: false, //  加载中
      passwordType: "password", // 是否密码明文的icon图标样式
      redirect: undefined, // 跳转路由
    };
  },
  mounted() {},
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  created() {
    console.log(this.$route.meta);
  },
  methods: {
    // 密码样式
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    handleLogin() {
      // 验证
      this.$refs.loginForm.validate((valid) => {
        // 验证是否正确
        if (valid) {
          this.loading = true;
          this.$store
            .dispatch("user/login", this.loginForm)
            .then(() => {
              this.queryMenuInfo();
            })
            .catch(() => {
              this.loading = false;
            });
        } else {
          console.log("账号或密码错误!!");
          return false;
        }
      });
    },
    // 登录后获取菜单权限
    queryMenuInfo() {
      this.$server
        .queryMenuInfo({ page: 1 })
        .then((res) => {
          const userList = res.user;
          console.log(res);
          let accessRoutes = [];
          store.commit("setMenuInfo", userList);
          accessRoutes = mapRouter(userList.sysMenuInfo, 1);
          accessRoutes.push(...constantRoutes);
          router.matcher = createRouter(accessRoutes).matcher; // 清空之前的路由信息
          localStorage.setItem("menuInfo", JSON.stringify(accessRoutes));
          localStorage.setItem("routesList", JSON.stringify(userList));
          router.options.routes.unshift(...accessRoutes);
          router.addRoutes(accessRoutes);
          this.$router.push({ path: this.redirect || "/" });
          this.loading = false;
        })
        .catch((err) => {
          this.loading = false;
          console.log(err);
        });
    },
  },
};
</script>

<style lang="scss" scoped>
$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    //color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  width: 100%;
  height: 100%;
  background: url("~@/assets/login/login_bg.png") no-repeat;
  background-size: 100% 100%;
  position: relative;
  &-content {
    // position: absolute;
    // top: 0;
    // right: 50%;
    // bottom: 0;
    // top: 0;
    // margin: auto;
    &-logo {
      width: 100%;
      display: flex;
      justify-content: center;
      margin-bottom: 20px;
      img {
        width: 120px;
        height: 120px;
      }
    }
    &-form {
      // background-color: rgba(255, 255, 255, 0.6);
      padding: 0px 40px;
      border-radius: 8px;
    }
  }
  .el-input {
    display: inline-block;
    height: 24px;
    width: 85%;

    input {
      background: transparent !important;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      //padding: 12px 5px 12px 15px;
      color: #454545 !important;
      height: 24px;
      caret-color: #454545 !important;
      &:-webkit-autofill {
        // 当选择下拉列表中存储的值以后
        box-shadow: 0 0 0px 1000px $cursor inset !important;
        color: #454545 !important;
        -webkit-text-fill-color: #454545 !important;
        background: #ffff00 !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    //background: rgba(0, 0, 0, 0.1);
    border-radius: 2px;
    color: #454545;
    background: #ffffff;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    //padding: 6px 5px 6px 15px;
    color: #306289;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      // 大标题
      font-size: 30px;
      color: #5acbf3;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    //top: 5px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
.el-form-item__content {
  line-height: 24px !important;
  display: flex;
  align-items: center;
  .svg-container {
    margin-left: 20px;
  }
}
input:-webkit-autofill,
textarea:-webkit-autofill,
select:-webkit-autofill {
  -webkit-box-shadow: 0 0 0px 1000px white inset !important;
  border: 1px solid #ccc !important;
  //background-image: inherit;
  //background-image: inherit;
  background: #ffffff !important;
}
.login-btn {
  width: 100%;
  background: #1b2648;
  display: flex;
  justify-content: center;
  border: none;
  letter-spacing: 10px;
  height: 45px;
  font-size: 20px;
  color: #4ca7ce;
}
.el-form-item__content {
  background: #11153b !important;
}
input.input-btn {
  background-color: #101439 !important;
}
.foot {
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  position: fixed;
  bottom: 30px;
  color: #35354d;
  font-size: 15px;
  img {
    width: 30px;
    vertical-align: middle;
  }
}
::v-deep .el-input__inner {
  background-color: #11153b !important;
  border: 1px solid #11153b;
}
::v-deep .el-form-item__content {
  background-color: #11153b !important;
  border-bottom: 2px solid #326d94;
}
::v-deep .el-form-item {
  border: 1px solid #11153b !important;
}
</style>
